package com.xinweisi.dataserver.timertask;

import com.xinweisi.dataserver.constant.Enumeration;
import com.xinweisi.dataserver.module.memberdata.user.UserService;
import com.xinweisi.dataserver.module.memberdata.user.domain.entity.UserEntity;
import com.xinweisi.dataserver.third.RedisService;
import com.xinweisi.dataserver.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * UserTimer
 *
 * @author lijie1
 * @version 1.0
 * @date 2020/7/21 0021 16:16
 */
@Slf4j
@Component
@EnableScheduling
public class UserTimer {

    @Autowired
    UserService userService;

    private RedisService redisService;
    @Autowired
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

    private final static Logger logger = Logger.getLogger(UserTimer.class);//给类初始化日志对象

    @Scheduled(cron = "*/1 * * * * ?")
    public void run() {
        int i = userService.getMemberListSize();
        if(i>UserUtil.size){
            logger.info("*******************我是测试");
            UserUtil.size = i;
            List<UserEntity> userEntityListNow = userService.getUserListNow();
            for (UserEntity obj:userEntityListNow) {
                UserUtil.userMap.put(obj.getAccountName(),obj);
            }
            List<UserEntity> userEntityListOld= userService.getUserListOld();
            for (UserEntity obj:userEntityListOld) {
                UserUtil.userMap.put(obj.getAccountName(),obj);
            }
        }
        Map<String, String> map = redisService.hgetAll(Enumeration.NEWUSER_KEY);

        if(map!=null && map.size()!=0){
            userService.addNewUsers(map);
        }
    }

}
